package com.maotaiapp.thread;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;


import com.alibaba.fastjson.JSONObject;
import com.maotai.dao.QiangGouDao;

public class MaoTaiAppDeleteAddressThread extends Thread{
	private Map<String, Object> mapList;
	private JSONObject jj;
	
	
	public Map<String, Object> getMapList() {
		return mapList;
	}

	public void setMapList(Map<String, Object> mapList) {
		this.mapList = mapList;
	}

	public JSONObject getJj() {
		return jj;
	}

	public void setJj(JSONObject jj) {
		this.jj = jj;
	}

	public MaoTaiAppDeleteAddressThread(Map<String, Object> mapList,
			JSONObject jj) {
		super();
		this.mapList = mapList;
		this.jj = jj;
	}

	@Override
	public void run() {
		QiangGouDao qgs = new QiangGouDao();
		Connection c = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
		
		c = DriverManager
			.getConnection("jdbc:mysql://localhost:3306/maotai?user=root&password=123456&useUnicode=true&characterEncoding=utf8");
			
		String id = mapList.get("id")+"";
		String auth = mapList.get("auth")+"";
		
		//璁块棶椤甸潰瀛樺叆鍊�
		InetSocketAddress proxyAddrGet = new InetSocketAddress(jj.getString("ip"),
				jj.getIntValue("port"));
        Socket underlyingGet = new Socket(new Proxy(Proxy.Type.SOCKS, proxyAddrGet));
        underlyingGet.connect(new InetSocketAddress("www.emaotai.cn", 443));
        
        Socket socketGet = (SSLSocket)((SSLSocketFactory)SSLSocketFactory.getDefault()).createSocket(underlyingGet,"www.emaotai.cn", 443,true);  
        
		OutputStreamWriter streamWriterGet = new OutputStreamWriter(socketGet.getOutputStream());    
        BufferedWriter bufferedWriterGet = new BufferedWriter(streamWriterGet);  
        StringBuffer sbGet = new StringBuffer();
        String data = "addressId="+id;
		sbGet.append("DELETE /yundt-application-trade-core/api/v1/yundt/trade/member/address/delete?appCode=1&_t="+System.currentTimeMillis()+" HTTP/1.1\r\n");  
		sbGet.append("Host: www.emaotai.cn\r\n");  
		sbGet.append("Connection: keep-alive" + "\r\n");  
		sbGet.append("Cache-Control: max-age=0" + "\r\n"); 
		sbGet.append("Origin: https://www.emaotai.cn" + "\r\n"); 
		sbGet.append("User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36\r\n");  
		sbGet.append("Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"+ "\r\n");    
		sbGet.append("auth:"+auth+"\r\n");  
		sbGet.append("Content-Type: application/x-www-form-urlencoded\r\n");  
		sbGet.append("terminalType:a1\r\n");  
		sbGet.append("appId:1\r\n"); 
		sbGet.append("Accept-Encoding: \r\n");    
		sbGet.append("Accept-Language:zh-CN,zh;q=0.9" + "\r\n");
		sbGet.append("Content-Length: " + data.length() + "\r\n");
		sbGet.append("\r\n");
		sbGet.append(data);
		sbGet.append("\r\n");
		 StringBuffer sbsbGet = new StringBuffer(sbGet);
	        sbsbGet.append("\r\n");
	        bufferedWriterGet.write(sbsbGet.toString());   
	        bufferedWriterGet.flush(); 
	          
	        BufferedInputStream streamReaderGet = new BufferedInputStream(socketGet.getInputStream());    
	        BufferedReader bufferedReaderGet = new BufferedReader(new InputStreamReader(streamReaderGet, "utf-8"));    
	        String line = null;   
	        //{"data":"","resultCode":0,"resultMsg":"success"}
	        while((line = bufferedReaderGet.readLine())!= null){ 
	        	System.out.println(line);
	        	if(line.contains("ResourceNotFound")){
	        		break;
	        	}
	        	if(line.contains("success")){
	        		qgs.deleteMaotaiAppAddress(mapList, c);
	        		break;
	        	}
	        }    
	        bufferedReaderGet.close();    
	        bufferedWriterGet.close();    
	        socketGet.close();  
	        
		} catch (Exception e1) {
			e1.printStackTrace();
		}finally{
			if(c!=null)
				try {
					c.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
	
}
